package spe.ui.menu.popup.actionprovider;

import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.WorkspaceJob;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.window.IShellProvider;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IWorkbenchCommandConstants;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.RefreshAction;
import org.eclipse.ui.actions.DeleteResourceAction;
import org.eclipse.ui.actions.WorkspaceModifyOperation;
import org.eclipse.ui.navigator.CommonActionProvider;
import org.eclipse.ui.navigator.ICommonActionExtensionSite;
import org.eclipse.ui.navigator.ICommonMenuConstants;

import spe.Activator;
import spe.model.spem.MethodLibrary;
import spe.ui.menu.popup.Messages;

public class DeleteActionProvider extends CommonActionProvider
{
	private DeleteResourceAction deleteAction;
	private Shell         shell;
	
	@Override
	public void init(ICommonActionExtensionSite aSite)
	{
		super.init(aSite);
		shell = aSite.getViewSite().getShell();
		makeActions();
	}
	
	@Override
	public void fillContextMenu(IMenuManager menu)
	{
		IStructuredSelection selection = (IStructuredSelection) getContext().getSelection();
		deleteAction.selectionChanged(selection);
		menu.appendToGroup(ICommonMenuConstants.GROUP_EDIT, deleteAction);
	}
	
	@Override
	public void fillActionBars(IActionBars actionBars) 
	{
		actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(),	deleteAction);
	} 
	
	private void makeActions()
	{
		IShellProvider sp = new IShellProvider() 
        {
            @Override
            public Shell getShell() {
                return shell;
            }
        };
        
		deleteAction = new DeleteResourceAction(sp)
		{
			@Override
			public void run()
			{
				IStructuredSelection selection = (IStructuredSelection)getContext().getSelection();
				Iterator iter = selection.iterator();
				Object element;
				while(iter.hasNext())
				{
					element = iter.next();
					if(IProject.class.isInstance(element))
					{
						try {
							boolean result = MessageDialog.openQuestion(shell, "Delete project", String.format("Do you really want to delete project %s ?", ((IProject)element).getName()));
							if(result)
								((IProject)element).delete(true, true, null);
						} catch (CoreException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						return;
					}
					
					if(MethodLibrary.class.isInstance(element))
					{
						return;
					}
				}
			}
		};
		
		deleteAction.setImageDescriptor(Activator.getImageDescriptorByPath("icons/delete_16x16.png"));
	}
	
	@Override
	public void updateActionBars()
	{
		IStructuredSelection selection = (IStructuredSelection)getContext().getSelection();
		deleteAction.selectionChanged(selection);
	}

}
